package mainScreen;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;

import sidebar.Sidebar;
import ui.ImageButton;
import ui.ImagePanel;

public class Profile implements MainScreen, ActionListener
{
	JPanel screenPanel;
	Sidebar sidebar;
	ImagePanel portrait;
	String[] charGen_portraitPaths;
	int charGen_portraitIndex;
	
	public Profile()
	{
		screenPanel = new JPanel();
		screenPanel.setLayout(null);
	}
	
	public void initiate(Sidebar sidebar)
	{
		// TODO: Initialize Profile
		this.sidebar = sidebar;
		JLabel label = new JLabel("Profile");
		label.setBounds(200, 200, 800, 100);
		screenPanel.add(label);
	}

	public void characterGeneration()
	{
		
		screenPanel.removeAll();
		
		//Set up the portrait carousel
		charGen_portraitIndex = 0;
		File folder = new File(sidebar.getOptions().GuildmasterPortraitPathMale);
		File[] files = folder.listFiles();
		charGen_portraitPaths = new String[files.length];
		for(int i=0; i<files.length; i++)
		{
			charGen_portraitPaths[i] = files[i].getAbsolutePath();
		}
		
		String defaultPortrait;
		defaultPortrait = charGen_portraitPaths[0];
		//TODO: skin-drive this?
		
		//Portrait
		portrait = new ImagePanel(defaultPortrait);
		portrait.setBounds(100, 100, 200, 200);
		portrait.setBackground(Color.black);
		portrait.setVisible(true);
		screenPanel.add(portrait);
		
		ImageButton.spawnButton ("button", "<", "CharGen_PortraitPrev", this, screenPanel, 5, 200, sidebar.getOptions());
		ImageButton.spawnButton ("button", ">", "CharGen_PortraitNext", this, screenPanel, 290, 200, sidebar.getOptions());
		
		//Name
		JTextField firstName = new JTextField("First Name", 40);
		firstName.setVisible(true);
		firstName.setBounds(100, 45, 80, 20);
		firstName.setEditable(true);
		screenPanel.add(firstName);
		
		JTextField lastName = new JTextField("Last Name", 40);
		lastName.setVisible(true);
		lastName.setBounds(230, 45, 80, 20);
		lastName.setEditable(true);
		screenPanel.add(lastName);
		
		//Gender
		JLabel genderLabel = new JLabel("Gender");
		genderLabel.setBounds(100, 310, 200, 20);
		screenPanel.add(genderLabel);
		
		JRadioButton genderMale = new JRadioButton("Male");
		genderMale.setBounds(100, 330, 100, 20);
		genderMale.setSelected(true);
		genderMale.setActionCommand("CharGen_GenderMale");
		genderMale.addActionListener(this);
		screenPanel.add(genderMale);
		
		JRadioButton genderFemale = new JRadioButton("Female");
		genderFemale.setBounds(200, 330, 100, 20);
		genderFemale.setActionCommand("CharGen_GenderFemale");
		genderFemale.addActionListener(this);
		screenPanel.add(genderFemale);
		
		ButtonGroup genderGroup = new ButtonGroup();
		genderGroup.add(genderMale);
		genderGroup.add(genderFemale);
		
		repaint();
	}
	public JPanel getPanel()
	{
		return screenPanel;
	}

	public String getScreenType()
	{
		return "Profile";
	}
	
	public void repaint()//repaint all relevant components
	{
		screenPanel.repaint();
		portrait.repaint();
	}
	public void endTurn()
	{
		//TODO: Profile end of turn - update stats probably
	}

	@Override
	public void actionPerformed(ActionEvent event)
	{
		String[] parsedEventCmd = event.getActionCommand().split("_");
		switch(parsedEventCmd[0])
		{
		case "CharGen":
			switch(parsedEventCmd[1])
			{
			case "GenderFemale":
				charGen_portraitIndex = 0;
				File folderF = new File(sidebar.getOptions().GuildmasterPortraitPathFemale);
				File[] filesF = folderF.listFiles();
				charGen_portraitPaths = new String[filesF.length];
				for(int i=0; i<filesF.length; i++)
				{
					charGen_portraitPaths[i] = filesF[i].getAbsolutePath();
				}
				portrait.changeImage(charGen_portraitPaths[charGen_portraitIndex]);
				break;
			case "GenderMale":
				charGen_portraitIndex = 0;
				File folderM = new File(sidebar.getOptions().GuildmasterPortraitPathMale);
				File[] filesm = folderM.listFiles();
				charGen_portraitPaths = new String[filesm.length];
				for(int i=0; i<filesm.length; i++)
				{
					charGen_portraitPaths[i] = filesm[i].getAbsolutePath();
				}
				portrait.changeImage(charGen_portraitPaths[charGen_portraitIndex]);
				break;
			case "PortraitNext":
				//similarly, gotta set up the portrait wraparound
				charGen_portraitIndex++;
				if(charGen_portraitIndex == charGen_portraitPaths.length)charGen_portraitIndex = 0;
				portrait.changeImage(charGen_portraitPaths[charGen_portraitIndex]);
				break;
			case "PortraitPrev":
				//set up wraparound
				charGen_portraitIndex--;
				if(charGen_portraitIndex == -1)charGen_portraitIndex = charGen_portraitPaths.length-1;
				portrait.changeImage(charGen_portraitPaths[charGen_portraitIndex]);
				break;
			default:
				System.err.println("CharGen action '" + parsedEventCmd[1] + "' isn't supported in mainSceen\\Profile.java");
			}
			break;
		default:
			System.err.println("Action '" + event.getActionCommand() + "' is not being handled by mainScreen\\Profile.jar");
		}
		
	}
}
